
import { _decorator, Component, Node, v3, Vec3, tween } from 'cc';
import Action3dManager, { ActionMngType } from '../../Script/Common/Action3dManager';
import yyComponent from '../../Script/Common/yyComponent';
const { ccclass, property } = _decorator;

//################ 碎片动画 ############
@ccclass('BreakItemAnim')
export class BreakItemAnim extends yyComponent {
    protected _initPos = null;
    protected _initAng = null;
    protected _initScale = null;
    //移动高度
    @property
    toH = 2;
    //目标位置
    @property
    toPos: Vec3 = v3();
    //旋转圈数
    @property
    rotateNum: number = 0.5;
    //时长 
    @property
    animTime = 1;
    //放大倍数-增强视觉效果
    @property
    scaleRate = 1.5;

    private _mng: Action3dManager = null;
    //分步骤移动动画属性
    private _toPosXZ: Vec3 = v3();
    private _toPosY: Vec3 = v3();

    public init() {
        //记录初始位置
        if (!this._initPos) {
            this._initPos = v3(this.node.position);
        }
        //记录初始角度
        if (!this._initAng) {
            this._initAng = v3(this.node.eulerAngles);
        }
        //记录初始缩放
        if (!this._initScale) {
            this._initScale = v3(this.node.scale);
        }

        this._mng = Action3dManager.getMng(ActionMngType.Level);
    }
    public reset() {
        this.stopAllAnim();

        //恢复初始位置+角度
        this.node.setPosition(this._initPos);
        this.node.eulerAngles = this._initAng;
        this.node.setScale(this._initScale);

        //重置参数
        this._toPosXZ.set(this._initPos.x, 0, this._initPos.z);
        this._toPosY.set(0, this._initPos.y, 0);
    }

    public play() {
        //xz轴移动
        let moveToX = Action3dManager.tweenTo(this.animTime, 'x', this.toPos.x * (0.5 + Math.random()));
        moveToX.easing(Action3dManager.easeSinOut());
        let moveToZ = Action3dManager.tweenTo(this.animTime, 'z', this.toPos.z * (0.5 + Math.random()));
        moveToZ.easing(Action3dManager.easeSinOut());
        let sqw = Action3dManager.spawn(moveToX, moveToZ);
        let seqXZ = Action3dManager.sequence(sqw, Action3dManager.callFun(() => { this.stopAllAnim(); }));
        this._mng.runAction(this._toPosXZ, seqXZ);

        //y轴移动
        let yHalfTime = this.animTime * 0.3;
        let moveToY1 = Action3dManager.tweenTo(yHalfTime, 'y', this.toH + Math.random());
        moveToY1.easing(Action3dManager.easeSinOut());
        let moveToY2 = Action3dManager.tweenTo(yHalfTime, 'y', 0);
        moveToY2.easing(Action3dManager.easeSinIn());
        let seqY = Action3dManager.sequence(moveToY1, moveToY2);
        this._mng.runAction(this._toPosY, seqY);

        //旋转
        let ang = v3(this._initAng);
        let tmpAng = this.rotateNum * 360;
        ang.add3f(tmpAng * Math.random(), tmpAng * Math.random(), tmpAng * Math.random());
        let rotateTo = Action3dManager.rotateTo(this.animTime, ang);
        rotateTo.easing(Action3dManager.easeSinOut());

        //缩放
        let _scale = v3(this._initScale);
        _scale.multiplyScalar(this.scaleRate);

        let scaleTo1 = Action3dManager.scaleTo(yHalfTime, _scale);
        scaleTo1.easing(Action3dManager.easeSinOut());

        let scaleTo2 = Action3dManager.scaleTo(yHalfTime, this._initScale);
        scaleTo2.easing(Action3dManager.easeSinIn());
        //合并 旋转+缩放
        let spawn = Action3dManager.spawn(rotateTo, Action3dManager.sequence(scaleTo1, scaleTo2));
        this._mng.runAction(this.node, spawn);

    }

    private _tmpPos = v3();
    public customUpdate(dt) {
        //设置属性
        this._tmpPos.set(this._toPosXZ.x, this._toPosY.y, this._toPosXZ.z);
        this.node.setPosition(this._tmpPos);

    }
    //停止动画
    public stopAllAnim() {
        if (!this._mng) return;
        this._mng.stopAllActions(this._toPosXZ);
        this._mng.stopAllActions(this._toPosY);
        this._mng.stopAllActions(this.node);

    }

}
